{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-19T01:17:44.893994Z",
     "start_time": "2025-04-19T01:17:44.882995Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "# 原始评分矩阵\n",
    "food = np.mat([[2,0,0,4,4],\n",
    "               [5,5,5,3,3],\n",
    "               [2,4,2,1,2],\n",
    "               [1,1,1,5,5]])\n",
    "\n",
    "# SVD分解\n",
    "U, sigma, Vt = np.linalg.svd(food)\n",
    "\n",
    "# 降维操作（取前2个特征）\n",
    "k = 2\n",
    "simple_food = U[:,:k] @ np.diag(sigma[:k]) @ Vt[:k,:]\n",
    "print(Vt)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.42143309 -0.42009655 -0.36326631 -0.49252063 -0.52093575]\n",
      " [-0.25478475 -0.53529229 -0.42751501  0.50877719  0.45488854]\n",
      " [ 0.66058728 -0.63425718  0.22940819  0.12795492 -0.30387777]\n",
      " [ 0.5666598   0.18627636 -0.72982216 -0.28669974  0.17134953]\n",
      " [ 0.         -0.31622777  0.31622777 -0.63245553  0.63245553]]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-19T01:17:45.098134Z",
     "start_time": "2025-04-19T01:17:45.081135Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "1b365c610aaaad6e",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-19T01:17:45.286459Z",
     "start_time": "2025-04-19T01:17:45.281457Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "ba62e3082c074bc8",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "6032d1adbb6762ec"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
